package com.financing.wap.service.impl;

 
 
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.financing.wap.activities.ActivitiesBusinessObject;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dao.IActivitesDao;
import com.financing.wap.dto.ActivitesDTO;
import com.financing.wap.dto.ActivitesExpandDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IAccountService;
import com.financing.wap.service.IActivitesService;
import com.financing.wap.service.IActivitieGiftService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DateUtil;
 


@Service("activitesService")
public class ActivitesServiceImpl implements IActivitesService,ApplicationContextAware{

	Logger logger =  Logger.getLogger(getClass());
 

	@Autowired
	private IActivitesDao activitesDao;
	
	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private IActivitieGiftService activitieGiftServiceImpl;
	
	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext  =  applicationContext;
		
	}

	@Override
	public ActivitesDTO searchActivitesByCode(String activitesCode) {
		 
		return activitesDao.searchActivitesByCode(activitesCode);
	}


	@Override
	public boolean isOutDateByCode(String code) {
		ActivitesDTO dto = activitesDao.searchActivitesByCode(code);
		Date startDate = dto.getActiviesStartTime();
		Date endDate = dto.getActiviesEndTime();
		Date currentDate = new Date();
		if(currentDate.before(startDate)||currentDate.after(endDate)){
			return true;
		}
		return false;
	}
	@Override
	public List<ActivitesExpandDTO> searchActivitesExpand(ActivitesExpandDTO bean)
			throws SQLException {
		return activitesDao.searchActivitesExpand(bean);
	}

	@Override
	public ResultMap operatingActivities(String code,UserDTO user) throws Exception {
		
		logger.info("in operatingActivities degin 注入活动开始");
		
		ResultMap result = new ResultMap();
		
		//判断活动时间
		if(isOutDateByCode(code)){
			logger.info("in operatingActivities  活动未开始或已过期");
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.ACTIVITIES_DATE_IS_OUT);
			result.setMessage("活动未开始或已过期");
			return result;
		}
		ActivitesDTO bean = searchActivitesByCode(code);
		
		//当前活动 -账户操作 
		if(accountService.operatingActivities(bean,user) <= 0){
			logger.info("in operatingActivities  账户操作失败");
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		logger.info("in operatingActivities end 注入活动结束");
		return result;
	}


	@Override
	public ResultMap ActivitiesJobExecution(String code) throws Exception {
		
		if(code.equals(Constants.ACTIVITIES_CODE_ZCSHB88)){
			return ActivitiesJobExecutionForSpecialTreatment(code);
		}
		
		ResultMap result = new ResultMap();
		
		//提取红包状态
		String aStatus = "01";
		//每次提取红包数
		Integer count = 500;
		
		//获取要执行的活动
		ActivitesDTO bean = activitesDao.searchActivitesByCode(code);
		
		//判断-活动是否结束，否  结束流程  是 对红包进行过期处理
		if(new Date().before(bean.getActiviesEndTime())){
			logger.info("in ActivitiesJobExecution code:"+code);
			logger.info("in ActivitiesJobExecution 活动进行中或未开始");
			return result;
		}
		
		//操作红包
		int size = count;
		while (size >= count) {
			size = activitieGiftServiceImpl.ActivitiesGiftJobExecution(code, aStatus, count);
		}
		
		return result;
	}
	
	/**注册送88红包 特殊处理
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public ResultMap ActivitiesJobExecutionForSpecialTreatment(String code) throws Exception {
		
		
		ResultMap result = new ResultMap();
		
		//提取红包状态
		String aStatus = "01";
		//每次提取红包数
		Integer count = 500;
		
		//获取要执行的活动
		ActivitesDTO bean = activitesDao.searchActivitesByCode(code);
		
		//判断-活动是否开始，否：结束流程  是：进入下一步
		if(new Date().before(bean.getActiviesStartTime())){
			logger.info("in ActivitiesJobExecution code:"+code);
			logger.info("in ActivitiesJobExecution 活动进行未开始");
			return result;
		}
		
		//操作红包
		int size = count;
		
		//活动 进行中 -处理流程
		if(DateUtil.nowBetweenstartTimeAndendTime(bean.getActiviesStartTime(),bean.getActiviesEndTime())){
			while (size >= count) {
				size = activitieGiftServiceImpl.ActivitiesGiftJobExecutionForSpecialTreatment(code, aStatus, count);
			}
		}//活动 结束 -处理流程
		else if(new Date().after(bean.getActiviesEndTime())){
			while (size >= count) {
				size = activitieGiftServiceImpl.ActivitiesGiftJobExecution(code, aStatus, count);
			}
		}
		
		return result;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResultMap ActivitiesJobExecutionObject(String businessType,
			Object object) throws Exception {
		logger.info("ActivitiesJobExecutionObject begin 注入活动开始!!!");
		logger.info("ActivitiesJobExecutionObject begin businessType:"+businessType);
		
		ActivitesExpandDTO activitesExpand = new ActivitesExpandDTO();
		activitesExpand.setBusinessType(businessType);
		List<ActivitesExpandDTO>  list =  searchActivitesExpand(activitesExpand);
		for(ActivitesExpandDTO activitesExpandDTO : list){
			try {
				//判断活动过期
				if(isOutDateByCode(activitesExpandDTO.getActiviesCode())){
					continue;
				}
				//判断是否存在处理类型WIN_TYPE
				if(activitesExpandDTO.getBusinessType().indexOf(businessType)<0){
					continue;
				}
				logger.info("ActivitiesJobExecutionObject begin ActiviesCode:"+activitesExpandDTO.getActiviesCode());
				logger.info("ActivitiesJobExecutionObject begin BusinessName:"+activitesExpandDTO.getBusinessName());
				
				//获取spring beanid 实例
				ActivitiesBusinessObject activitiesBusinessObject =  (ActivitiesBusinessObject) applicationContext.getBean(activitesExpandDTO.getBusinessName());
				
				//根据类的放射执行 类的 activitesExpandDTO.getBusinessType() 方法。
				Class bean = activitiesBusinessObject.getClass();
				Method method = bean.getMethod(activitesExpandDTO.getBusinessType(),Object.class);
				method.invoke(activitiesBusinessObject,object);
			} catch (Exception e) {
				logger.error("ActivitiesJobExecutionObject error", e);
				continue;
			}
		}
		logger.info("ActivitiesJobExecutionObject end 注入活动结束!!!");
		return null;
	}
	 
}
